package org.tis.tools.senior.module.developer.util;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

public class Job {
    //jenkins登录账号
    public static String username = "tools";
    //jenkins登录密码
    public static String password = "tools";
    //jenkins登录url
    public static String jenkinsUrl = "http://106.15.33.239:8080";
    public static String buildToken = "52f01bb6a29487903be17ee913361291";

//    public static void main(String[] args) throws IOException {
//        Job job = new Job();
//        //job.buildJob("YourJobName",true,null);
//        //job.buildJob("YourJobName",true,"param1=value1&param2=value2&param3=value3");
////        job.buildJob("JobText", false, "token=token");
//
//        job.scrape("http://106.15.33.239:8080/job/JobText/build?token=token", username, password);
//    }

    /*
     * 调用jenkins接口，构建job
     * 1、jobName:需要构建的jenkins工程的名字
     * 2、buildJobParams：构建jenkins工程时需要的参数
     * 3、isNeedParams：jenkins工程的类型，如果jenkins工程带参数，isNeedParams=ture(isNeedParams=true时，buildJobParams生效，参数格式：key1=value1&key2=value2...，不填写参数时jenkins使用默认参数)
     *                        如果jenkins工程不带参数，isNeedParams=false(isNeedParams=false时，buildJobParams不生效，可以随便填写)
     */
    public void buildJob(String jobName, Boolean isNeedParams, String buildJobParams) throws IOException {
        //jenkins构建job的url
        String jenkinsBuildUrl = jenkinsUrl + "/job/" + jobName + "/build?token=token";
        if (isNeedParams == true) {
            jenkinsBuildUrl = jenkinsUrl + "/job/" + jobName + "/buildWithParameters" + "?" + buildJobParams;
        }
        URI uri = URI.create(jenkinsBuildUrl);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
        // Create AuthCache instance
//        AuthCache authCache = (AuthCache) new BasicAuthCache();
        BasicAuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        HttpPost httpPost = new HttpPost(uri);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
//        httpClient.
        HttpResponse response = httpClient.execute(host, httpPost, localContext);
        EntityUtils.toString(response.getEntity());

    }

    public String scrape(String urlString, String username, String password) throws ClientProtocolException, IOException {
        URI uri = URI.create(urlString);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
//        HttpGet httpGet = new HttpGet(uri);
        HttpPost httpPost = new HttpPost(uri);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpResponse response = httpClient.execute(host, httpPost, localContext);

        return EntityUtils.toString(response.getEntity());
    }
}
